/**
 * Created by dell on 2018/12/11.
 */
const db = require('./db.js');
//提供所有的图书信息
exports.allBooks = (req,res)=>{
    let sql = 'select * from book';
    db.base(sql,null,(result)=>{
        res.json(result);
    });
}
//添加图书
exports.addBook = (req,res)=>{
    let info = req.body;
    let sql = 'insert into book set ?';
    db.base(sql,info,(result)=>{
        if(result.affectedRows == 1){
            res.json({flag:1});
        }else{
            res.json({flag:2});
        }
    })
}
//查询
exports.getBookById = (req,res)=>{
    let info = res.query.id;
    let sql = 'select * from book';
    let data = [id];
    db.base(sql,data,(result)=>{
       res.json(result[0]);
    });
}
//更新
exports.editBook = (req,res)=>{
    let info = res.body;
    let sql = 'updata book set name=?,author=?,category=?,description=? where id=?';
    let data = [info.name,info.author,info.category,info.description,info.id];
    db.base(sql,data,(result)=>{
        if(result.affectedRows == 1){
            res.json({flag:1});
        }else{
            res.json({flag:2});
        }
    });
}
//删除
exports.deleteBook = (req,res)=>{
    let id = req.query.id;
    let sql = 'delete from book where id=?';
    let data = [id];
    db.base(sql,data,(result)=>{
       if(result.affectedRows == 1){
           res.json({flag:1});
       }else{
           res.json({flag:2});
       }
    });
}